<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 状态管理
 * @author zxj
 *
 */
class Status extends MY_Controller {
    function __construct() {
        parent::__construct();
        //加载权限模型
        $this->load->model('Status_model');
        //$this->load->model('Role_model');
    }


    /**
     * 列表
     */
    function index(){
        $this->Status_model->checkTables('mt_product_status');
        $dataList = $this->Status_model->get_status();
        gsh_output($dataList);

    }


    function add(){

    }


    /**
     * 新增用户
     */
    function add2(){
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //插入数据
        $submits = gp('submits');
        if ($submits) {
            //获取表字段
            $fieldsList = $this->db->list_fields($this->t_user);
            //组织插入的数据
            $indata = array();
            foreach (gp() as $key=>$val) {
                if(($val || $val===0) && in_array($key,$fieldsList)){
                    $indata[$key] = $val;
                }
            }
            $this->orderModel->insert($this->t_user,$indata);
            js_goto(blang_url('users/member/'),'新增成功!');
        }else{
            //角色类型
            $roleList = array('service'=>'客服','repairer'=>'维修员');
            $data['roleList'] = $roleList;
            //视图以及样式
            $this->load->view('defaults/member_post',$data);
            $this->load->view('defaults/common/footer');
        }
    }

    /**
     * 新增合作客户
     */
    public function addhz($page='',$type='',$id=''){
        $gp=gp();
        switch($type){
            case 'add':
                $this->users_model->addcustomers($gp);
                break;
            case 'del':
                $this->users_model->deletecustomerslist($id);
                break;
            case 'select':
                $keyword=array(
                    'name'=>$gp['keyword'],
                    'email'=>$gp['keyword'],
                    'tel'=>$gp['keyword'],
                );
                break;
        }
        $data['khlist']=$this->users_model->customerslist($keyword);
        $this->load->view('defaults/customers_list',$data);
        $this->load->view('defaults/common/footer');
    }

    /**
     * 客户预存款列表
     */
    public function khdepositlist(){
        $gp=gp();
        if($gp['keyword']){
            $keyword=array(
                'email'=>$gp['keyword'],
                'name'=>$gp['keyword'],
            );
        }
        $data['depositrecordlist']=$this->users_model->depositrecordlist($keyword);
        $this->load->view('defaults/customers_deposit_list',$data);
        $this->load->view('defaults/common/footer');
    }

    /**
     * 同步存款(接口那边就是扯蛋蛋)
     */
    public function syncupdatedeposit(){
        $proxy = new SoapClient('http://www.tomrepair.es/api/soap/?wsdl');
        $sessionId = $proxy->login('Pandawilljason', 'pandawillJason@#23&Pdw^jor23dan*');
        $postData = array($is_sync = 0,$qty = 100,$page=1, $data = array(),$transaction_type = 1);
        $result = $proxy->call($sessionId, 'weixiuzhan_activity.getsummaryfortransaction',$postData);#获取paypal支付的支付记录(日志)
        $data=array();
        $amount=0;
        $email=array();
        if($result) {
            foreach ($result as $row) {
                //字符串替换  替换掉接口那边写得扯JB蛋玩意 多余
                $row['comment'] = str_replace('====isRes :""', '', $row['comment']);
                //对交易记录进行数组转换
                $comment = json_decode($row['comment']);
                if (!is_array($comment)) {
                    $comment = get_object_vars($comment);
                }
                //匹配金额保存字段  接口那边的扯JB蛋玩意 多余
                if ($comment['amount']) {
                    $amount = $comment['amount'];
                } elseif ($comment['amt']) {
                    $amount = $comment['amt'];
                } elseif ($comment['payment_gross']) {
                    $amount = $comment['payment_gross'];
                }
                $data['money'] = $amount;
                $data['email'] = $row['customer_email'];
                $data['time'] = $row['expiration_date'];
                $this->users_model->adddepositrecord($data);
                $postData =  array('id'=>$row['transaction_id'],'is_sync'=>1);
                $result = $proxy->call($sessionId, 'weixiuzhan_activity.transactionupdate',array($postData));
            }
        }
    }
    /**
     * 审核客户存款
     */
    public function auditdeposit($id,$email,$money){
        $email=base64_decode($email);
        $up=$this->users_model->auditdeposit($id);
        if($up){
            $em=$this->users_model->verificationemail($email);
            if($em){
                $this->users_model->upddeposit($email,$money);
            }else{
                $data=array(
                    'email'=>$email,
                    'money'=>$money,
                );
                $this->users_model->adddeposit($data);
            }
            $this->postmoney($email,$money);
            $this->depositrecordlist();
        }
    }
    public function td(){
        $this->users_model->td();
    }
    /**
     * 存款记录列表
     */
    public function depositrecordlist(){
        $gp=gp();
        $this->syncupdatedeposit();
        if($gp['keyword']){
            $keyword=array(
                'email'=>$gp['keyword'],
                'name'=>$gp['keyword'],
            );
        }
        $data['depositrecordlist']=$this->users_model->depositlistrecord($keyword,"status = '0' desc");
        $this->load->view('defaults/customers_deposit_record_list',$data);
        $this->load->view('defaults/common/footer');
    }
    public function postmoney($email,$amount){
        $proxy = new SoapClient('http://www.tomrepair.es/api/soap/?wsdl');
        $sessionId = $proxy->login('Pandawilljason', 'pandawillJason@#23&Pdw^jor23dan*');
        $type = 'update';
        $Data = array('amount'=>$amount);
        $postData = array($email,$type,$Data);
        $result = $proxy->call($sessionId, 'weixiuzhan_activity.summaryforaction',$postData);
        return $result;
    }
    /**
     * 修改会员状态
     * @param string $id
     * @param string $enable
     * @param string $page
     */
    function set_active($id='',$enable='',$page='') {
        $page = max(intval($page),1);
        $enable?$enable=0:$enable=1;
        $this->common_model->update($this->t_user,array('active'=>$enable),array('id'=>intval($id)));
        redirect(blang_url('users/member/index/?page='.$page));
    }

    /**
     * 修改会员外网权限
     * @param string $id
     * @param string $enable
     * @param string $page
     */
    function set_isopen($id='',$enable='',$page='') {
        $page = max(intval($page),1);
        $enable?$enable=0:$enable=1;
        $this->common_model->update($this->t_user,array('is_open'=>$enable),array('id'=>intval($id)));
        redirect(blang_url('users/member/index/?page='.$page));
    }


}
